# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/8/4 4:34 下午
# @Author  : 白辉东
# @Mail    : masterbai2018@outlook.com
# @File    : second_nlu_test.py
# @Software: PyCharm

import requests
import json
import pandas as pd

url = 'https://api.ximalaya.com/smart-os-nlu/screen-nlu/list'
headers = {'Content-Type': 'application/json'}


def request_data(intention):
    app = {'id': '1477', 'name': 'os.client.000001_1477'}
    domain = {'id': '2992', 'name': 'habit_formation'}

    tmp_next = [
        {'app': app,
         'domain': domain,
         'intention': {'id': '102677', 'name': 'think_reply'},
         'replies': []}
    ]

    history_response = {'app': app,
                        'domain': domain,
                        'domainType': 'RULE_DOMAIN',
                        'intention': intention,
                        'source': 'SYSTEM',
                        'next': tmp_next}

    data_dict = {'sessionId': 'cd3b285b98334c51913171bb6844f53e',
                 'uid': 5,
                 'question': '放首歌',
                 'app': app,
                 'needMulti': 'false',
                 'historyResponse': [history_response]}

    return json.dumps(data_dict)


if __name__ == '__main__':
    data = pd.read_excel('./habit_list.xls', index_col=False, sheet_name='Sheet1', keep_default_na=False)
    for i in range(0, len(data)):
        habit_name, TTS, _, tmp_intention_positive, _, _ = data.iloc[i]
        intention = eval(tmp_intention_positive)
        body = request_data(intention)
        re = requests.post(url=url, headers=headers, data=body)
        response_data = re.json()[0]
        result_name = response_data['intention']['name']
        print('habit_name:', habit_name)
        print('TTS:', TTS)
        print('intention:', intention)
        print('result_name:', result_name)
